/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyyoona7.picker.util;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Picker;

/**
 * Picker滚动帮助类
 *
 * @since 2021-04-23
 */
public class PickerScrollHelper implements AnimatorValue.ValueUpdateListener {
    private AnimatorValue mAnimatorValue;

    private float distance;
    private float currentDistance;

    private float offsetV = 0;

    private float offsetY = 0;

    private static final int itemDely = 150;

    private float itemHeight;

    private int currentItemIndex;

    private Picker mPicker;

    private int finalIndex;

    private boolean isRun = false;

    /**
     * 构造器
     *
     * @param picker 需要滚动的控件
     */
    public PickerScrollHelper(Picker picker) {
        mPicker = picker;

        mAnimatorValue = new AnimatorValue();
        mAnimatorValue.setCurveType(Animator.CurveType.ACCELERATE);
        mAnimatorValue.setValueUpdateListener(this);

        mAnimatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                offsetV = 0;
                isRun = false;
                mPicker.setValue(finalIndex);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
    }

    /**
     * 滑动到指定下标
     *
     * @param index picker的下标值
     * @param rate 滑动速率0~1
     */
    public void scrollToIndex(int index, float rate) {
        if (isRun) {
            return;
        }
        finalIndex = index;
        isRun = true;
        currentItemIndex = mPicker.getValue();
        itemHeight = (mPicker.getHeight() + mPicker.getPaddingStart() + mPicker.getPaddingEnd()) * 1.0f
                / mPicker.getSelectorItemNum();
        distance = (index - currentItemIndex) * itemHeight;
        offsetV = 0;
        currentDistance = 0;
        int duration = (int) (itemDely * rate * Math.abs(index - currentItemIndex));
        mAnimatorValue.setDuration(duration);
        mAnimatorValue.start();
    }

    /**
     * onUpdate
     *
     * @param animatorValue
     * @param value
     */
    @Override
    public void onUpdate(AnimatorValue animatorValue, float value) {
        float mOffsetDistance = 0;
        mOffsetDistance = ((value - offsetV) * distance) + offsetY;
        if (Math.abs(currentDistance + mOffsetDistance) > Math.abs(distance)) {
            mOffsetDistance = 0;
        }
        if (value == 1) {
            mPicker.setValue(finalIndex);
            return;
        }
        offsetY = (float) (mOffsetDistance - (int)mOffsetDistance);
        mPicker.scrollBy(0, (int)mOffsetDistance);
        offsetV = value;
        currentDistance += (int)mOffsetDistance;
    }
}
